<template>
  <div class="pdf-page">
    <iframe v-show="!loading" :src="src" frameborder="0" id="pdf" width="100%"></iframe>
    <div v-show="loading" class="no-data">
      <loading1 :emptyText='emptyText'/>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    publicInfo: {

    },
    btnActive: {
      type: Number
    },
    type: {
      type: String
    }
  },
  data() {
    return {
      src: '',
      emptyText: '加载中...',
      loading:true,
    }
  },
  methods: {
    init() {
      this.$getBlob(this.$Url.show.pdf, {
        pn: this.$route.query.id
      }).then(res => {
        let blobSize = res.data.size
        if (blobSize < 500) { // 如果pdf没有，则跳转到ep地址
          this.$get(this.$Url.show.pdf, {
            pn: this.$route.query.id
          }).then(res => {
            this.src = res.data.epUrl
          })
        } else {
          let urlPdf  = window.URL.createObjectURL(new Blob([res.data]))
          this.src = './static/pdfjs/web/viewer.html?file=' + encodeURIComponent(urlPdf)
        }
        this.loading = false
        
      }).catch(err => {})
    },
    // 标准检索-pdf
    initStandard() {
      if (this.publicInfo.pdf) {
        this.$getBlob(this.$Url.show.standardpdf, {
          pdfname: this.publicInfo.pdf
        }).then(res => {
          let urlPdf  = window.URL.createObjectURL(new Blob([res.data]))
          this.src = './static/pdfjs/web/viewer.html?file=' + encodeURIComponent(urlPdf)
          this.loading = false
        }).catch(err => {})
      } else {
        this.emptyText = '暂无数据'
      }
    },
  },
  watch: {
    btnActive() {
      if (this.btnActive === 3 && this.src === '') {
        this.init()
      } else if (this.$route.query.type === "standard") { // 标准
        this.initStandard()
      }
    },
  },
  mounted() {
    // this.init()
  }
}
</script>

<style lang="scss" scoped>
.pdf-page {
  position: relative;
  height: 800px;
  padding-top: 20px;
}
#pdf {
  height: 100%;
}
.no-data {
  position: relative;
  font-size: 14px;
  color: $color6;
  margin-top: 50px;
}
</style>